<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-button class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">添加</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row >

		<el-table-column align="center" label="菜单ID" prop="menuid" />
		<el-table-column align="center" label="菜单名称" prop="menuname" />
		<el-table-column align="center" label="图片" prop="imgurl">
			<template width="50" slot-scope="scope">
				<img style="width:50px;height:50px;border:none;" :src="scope.row.imgurl" />
			</template>
		</el-table-column>
      <el-table-column align="center" label="别名" prop="alias" />

      <el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
      
    </el-table>

    <!-- 添加或修改对话框 -->

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      
      <el-form ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        
        <el-form-item label="菜单名称" prop="menuname">
          <el-input v-model="dataForm.menuname " />
        </el-form-item>
        
		<el-form-item label="图片" prop="imgurl">
			<el-upload
			:headers="headers"
			action="http://localhost:8090/upload/uploadBrowser"
			:show-file-list="false"
			:on-success="uploadProductUrl"
			class="avatar-uploader"
			accept=".jpg,.jpeg,.png,.gif">
				<img v-if="dataForm.imgurl" :src="dataForm.imgurl" class="avatar">
				<i v-else class="el-icon-plus avatar-uploader-icon " />
			</el-upload>
		</el-form-item>
        
        <el-form-item label="别名" prop="alias">
          <el-input v-model="dataForm.alias " />
        </el-form-item>
        
      </el-form>
      
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">确定</el-button>
        <el-button v-else type="primary" @click="updateData">确定</el-button>
      </div>
      
    </el-dialog>


  </div>
</template>

<style scoped>
.filter-item{
  margin-left: 100px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #20a0ff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}
.avatar {
  width: 145px;
  height: 145px;
  display: block;
}
</style>

<script>
import { uploadProductUrl } from '@/api/uploadBrowser.js'
import { selmenu,del,add,update } from '@/api/menu.js'
import { uploadPath } from '@/api/storage'
import { getToken } from '@/utils/auth'

export default {
  name: 'Menu',
  data() {
    return {
      uploadPath,
      list: [], //数据
      dataForm: [{
        menuid: 0,
        menuname: '',
        imgurl: '',
        alias: ''
      }],
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      rules: {
        name: [{ required: true, message: '类目名不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    headers() {
      return {
        'X-Litemall-Admin-Token': getToken()
      }
    }
  },
  created() {
    this.getList()
    // this.getCatL1()
  },
  methods: {
	uploadProductUrl: function(response) {
		console.log(1)
		console.log(response)
		this.dataForm.imgurl = response.data
	},
    getList() {
      selmenu().then((res) => {
		console.log(res)
        this.list = res.data.data
      })


    },
    resetForm() {
      this.dataForm = {
        menuid: 0,
        menuname: '',
        imgurl: '',
        alias: ''
      }
    },
   handleCreate() {
     this.resetForm()
     this.dialogStatus = 'create'
     this.dialogFormVisible = true
    this.$nextTick(() => {
      this.$refs['dataForm'].clearValidate()
    })
   },
    uploadIconUrl: function(response) {
      this.dataForm.iconUrl = response.data.url
    },
    uploadPicUrl: function(response) {
      this.dataForm.picUrl = response.data.url
    },
    //增加
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          add(this.dataForm)
            .then(() => {
              this.getList()
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '添加成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.errmsg
              })
            })
        }
      })
    },
    handleUpdate(row) {
      this.dataForm = row //对话框中数据
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    //更改
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          update(this.dataForm)
            .then(() => {
              this.getList()
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '更新成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.errmsg
              })
            })
        }
      })
    },
    handleDelete(row) {
			if(window.confirm('您确定要删除吗？')){
				del(row)
				.then(response => {
				  this.getList()
				})
			}
    }
   }
}
</script>
